package com.peak.distribution.web.validator.method;

import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

import com.peak.distribution.web.validator.ValidateRuleMethod;
import com.peak.spring.boot.tools.Converts;
import com.peak.spring.boot.tools.Emptys;

public class ImageSuffix implements ValidateRuleMethod {

	@Value("${validate.rule.image}")
	private String errorMessage;
	
	private String suffix = "jpg,gif,png,bmp";

	@Override
	public boolean execute(Object value) {
		String path = Converts.toString(value);
		String suffix = StringUtils.substringAfterLast(path, ".");
		String[] suffixs = this.suffix.split(",");
		if (ArrayUtils.contains(suffixs, suffix)) {
			return true;
		}
		return false;
	}

	@Override
	public String getErrorMessageTemplate() {
		return Emptys.isEmpty(this.errorMessage) ? "{0}不是合法的图片格式，请使用下列图片格式：" + this.suffix : this.errorMessage;
	}

	@Override
	public void setParams(Map<String, Object> param) {
		this.suffix = Converts.toString(param.get("suffix"));
	}

}
